package com.yang.qqservice.service;

import com.yang.common.Message;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description: 用于管理和客户端通信的线程类
 * @Author: YANG QIANG
 * @Time: 2022-12-20 21:33
 */
public class ManageClientThread {
    private static ConcurrentHashMap<String, ServerConnectClientThread> hm = new ConcurrentHashMap<>();

    // 离线的消息集合 key:用户useId, value: 消息集合
    private static ConcurrentHashMap<String, List<Message>> offLineHm = new ConcurrentHashMap<>();


    public static void addOffLineHm(String userId, Message message){
        offLineHm.putIfAbsent(userId, new ArrayList<>());
        offLineHm.get(userId).add(message);
    }

    public static List<Message> getOffLineHm(String userId){
        return offLineHm.get(userId);
    }

    public static void removeOffLineHm(String userId){
        offLineHm.remove(userId);
    }

    public static void addServerConnectClientThread(String userId, ServerConnectClientThread clientThread){
        hm.put(userId, clientThread);
    }

    public static ServerConnectClientThread getServerConnectClientThread(String userId){
        return hm.get(userId);
    }

    public static void remove(String userId){
        hm.remove(userId);
    }

    public static ConcurrentHashMap<String, ServerConnectClientThread> getHm() {
        return hm;
    }

    // 返回用户列表
    public static String getOnlineUsers(){
        Iterator<String> iterator = hm.keySet().iterator();
        String str = "";
        while (iterator.hasNext()){
            str += iterator.next() + " ";
        }
        return str;
    }
}
